package cn.iocoder.yudao.module.ao.controller.admin.academicyear;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ao.controller.admin.academicyear.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.academicyear.AcademicYearDO;
import cn.iocoder.yudao.module.ao.service.academicyear.AcademicYearService;

@Tag(name = "管理后台 - 学年及班级信息")
@RestController
@RequestMapping("/ao/academic-year")
@Validated
@Slf4j
public class AcademicYearController {

    @Resource
    private AcademicYearService academicYearService;

    @PostMapping("/create")
    @Operation(summary = "创建学年及班级信息")
    @PreAuthorize("@ss.hasPermission('ao:academic-year:create')")
    public CommonResult<Long> createAcademicYear(@Valid @RequestBody AcademicYearSaveReqVO createReqVO) {
        return success(academicYearService.createAcademicYear(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新学年及班级信息")
    @PreAuthorize("@ss.hasPermission('ao:academic-year:update')")
    public CommonResult<Boolean> updateAcademicYear(@Valid @RequestBody AcademicYearSaveReqVO updateReqVO) {
        academicYearService.updateAcademicYear(updateReqVO);
        return success(true);
    }

    // 删除功能已移除，防止误删重要的学年及班级基础数据
    // @DeleteMapping("/delete")
    // @Operation(summary = "删除学年及班级信息")
    // @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('ao:academic-year:delete')")
    // public CommonResult<Boolean> deleteAcademicYear(@RequestParam("id") Long id) {
    //     academicYearService.deleteAcademicYear(id);
    //     return success(true);
    // }

    // @DeleteMapping("/delete-list")
    // @Parameter(name = "ids", description = "编号", required = true)
    // @Operation(summary = "批量删除学年及班级信息")
    // @PreAuthorize("@ss.hasPermission('ao:academic-year:delete')")
    // public CommonResult<Boolean> deleteAcademicYearList(@RequestParam("ids") List<Long> ids) {
    //     academicYearService.deleteAcademicYearListByIds(ids);
    //     return success(true);
    // }

    @GetMapping("/get")
    @Operation(summary = "获得学年及班级信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ao:academic-year:query')")
    public CommonResult<AcademicYearRespVO> getAcademicYear(@RequestParam("id") Long id) {
        AcademicYearDO academicYear = academicYearService.getAcademicYear(id);
        return success(BeanUtils.toBean(academicYear, AcademicYearRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得学年及班级信息分页")
    @PreAuthorize("@ss.hasPermission('ao:academic-year:query')")
    public CommonResult<PageResult<AcademicYearRespVO>> getAcademicYearPage(@Valid AcademicYearPageReqVO pageReqVO) {
        PageResult<AcademicYearDO> pageResult = academicYearService.getAcademicYearPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AcademicYearRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出学年及班级信息 Excel")
    @PreAuthorize("@ss.hasPermission('ao:academic-year:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAcademicYearExcel(@Valid AcademicYearPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AcademicYearDO> list = academicYearService.getAcademicYearPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "学年及班级信息.xls", "数据", AcademicYearRespVO.class,
                        BeanUtils.toBean(list, AcademicYearRespVO.class));
    }



}